//
//  SEGShopClassViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/10.
//

#import "SEGShopClassViewModel.h"
#import "SEGSHManager.h"

@interface SEGShopClassViewModel ()
@property (nonatomic, strong) SEGSHManager *sHManager;
@end

@implementation SEGShopClassViewModel

- (instancetype)init {
    if (self = [super init]) {
        self.dataArray = [[NSMutableArray alloc] init];
        [self seg_createCommand];
    }
    return self;
}

- (void)seg_createCommand {
    @weakify(self);
    self.command = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSString *classifyId) {
        
       return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            [self.sHManager requestGoodsClassifyWithClassifyId:classifyId finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (isSuccessed) {
                    for (NSDictionary *dic in [result valueForKey:@"data"]) {
                        [self.dataArray addObject:dic];
                    }
                    [subscriber sendNext:nil];
                }else {
                    [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                }
                [subscriber sendCompleted];
            }];
            
            return nil;
        }];
    }];
}



- (SEGSHManager*)sHManager {
    if (!_sHManager) {
        _sHManager = [[SEGSHManager alloc] init];
    }
    return _sHManager;
}

@end
